<template>
  <div class="component-one">
    <!-- 文本输入框 input -->
    <el-input
      v-if="formItem.componentType === 'text'"
      v-model="formItem.setValue"
      :placeholder="`请填写`"
      style="width: 100%"
      :disabled="forbidEditFieldFn(formItem.setKey)"
      @input="handleChange"
    ></el-input>

    <!-- 选择器 单选或者多选 select -->
    <el-select
      v-if="
        formItem.componentType === 'select' ||
        formItem.componentType === 'multipleSelect'
      "
      v-model="formItem.setValue"
      :multiple="formItem.componentType === 'multipleSelect'"
      :collapse-tags="formItem.componentType === 'multipleSelect'"
      :placeholder="`请选择`"
      clearable
      :disabled="forbidEditFieldFn(formItem.setKey)"
      style="width: 100%"
      @change="handleChange"
    >
      <el-option
        v-for="(item, index) in formItem.customData"
        :key="index"
        :label="item.value"
        :value="item.key"
      ></el-option>
    </el-select>

    <!-- 选择器 单选 selectStr, 传过去的key和value都是中文 -->
    <el-select
      v-if="formItem.componentType === 'selectStr'"
      v-model="formItem.setValue"
      :placeholder="`请选择`"
      :disabled="forbidEditFieldFn(formItem.setKey)"
      style="width: 100%"
      @change="handleChange"
    >
      <el-option
        v-for="(item, index) in formItem.customData"
        :key="index"
        :label="item.value"
        :value="item.value"
      ></el-option>
    </el-select>

    <!-- 日期选择器 date-picker -->
    <!-- :placeholder="`请选择${formItem.setName}`" -->
    <el-date-picker
      v-if="formItem.componentType === 'datePicker'"
      v-model="formItem.setValue"
      type="date"
      value-format="yyyy-MM-dd"
      :placeholder="`请选择日期`"
      :disabled="forbidEditFieldFn(formItem.setKey)"
      @change="handleChange"
    >
    </el-date-picker>

    <!-- 日期范围选择器 date-picker -->
    <el-date-picker
      v-if="formItem.componentType === 'datePickerRange'"
      v-model="formItem.setValue"
      type="daterange"
      :clearable="false"
      value-format="yyyy-MM-dd"
      start-placeholder="开始日期"
      end-placeholder="结束日期"
      range-separator="-"
      :disabled="forbidEditFieldFn(formItem.setKey)"
      style="width: 100%"
      @change="handleChange"
    >
    </el-date-picker>

    <!-- 时间选择器 time-picker -->
    <el-time-picker
      v-if="formItem.componentType === 'timePicker'"
      v-model="formItem.setValue"
      value-format="HH:mm"
      :picker-options="{
        start: '00:00',
        step: '00:01',
        end: '24:00'
      }"
      :placeholder="`请选择时间`"
      :disabled="forbidEditFieldFn(formItem.setKey)"
      style="width: 100%"
      @change="handleChange"
    >
    </el-time-picker>

    <!-- 时间选择器 time-picker -->
    <el-time-picker
      v-if="formItem.componentType === 'timePickerRange'"
      is-range
      value-format="HH:mm:ss"
      v-model="formItem.setValue"
      range-separator="-"
      start-placeholder="开始时间"
      end-placeholder="结束时间"
      :placeholder="`请选择${formItem.setName}`"
      :disabled="forbidEditFieldFn(formItem.setKey)"
      style="width: 100%"
      @change="handleChange"
    >
    </el-time-picker>
  </div>
</template>

<script>
export default {
  props: {
    formItem: {
      type: Object,
      default: () => {}
    },
    forbidEditField: {
      type: Array,
      default: () => []
    }
  },
  data() {
    return {
      setValue: []
    };
  },
  methods: {
    handleChange(val) {
      this.formItem.setValue = val;
      if (this.formItem.componentType === 'datePickerRange') {
        if (this.formItem.setValue) {
          this.$emit(
            'change',
            this.formItem.setKey,
            JSON.stringify(this.formItem.setValue.join(','))
          );
        } else {
          this.$emit('change', this.formItem.setKey, '');
        }
      } else {
        this.$emit(
          'change',
          this.formItem.setKey,
          JSON.stringify(this.formItem.setValue)
        );
      }
    },
    forbidEditFieldFn(key) {
      return this.forbidEditField.includes(key);
    }
  }
};
</script>

<style lang="scss" scoped></style>
